package org.l.qiao.base;

import org.apache.log4j.Logger;
import org.l.qiao.annotation.Key;

/**
 * Created by 严秋旺 on 2015/6/10.
 */
public class Convert {
    public static final Logger LOGGER = Logger.getLogger(Convert.class);

    /**
     * 按对象类型转换字符串
     *
     * @param type  LType对象类型
     * @param value 需要转换字符串
     * @return 转换后的对象
     */
    public static final Object parse(int type, String value) {
        LType lType = getLType(type);
        return lType.parse(value);
    }

    /**
     * 根据注解转换字符串
     *
     * @param key   注解Key
     * @param value 需要转换字符串
     * @return 转换后的对象
     */
    public static final Object parse(Key key, String value) {
        LType lType = getLType(key.valueType());
        return lType.parse(key, value);
    }

    /**
     * 获取LType类型
     *
     * @param type
     * @return
     * @throws UnknowLTypeException
     */
    public static LType getLType(int type) {
        LType lType = null;

        switch (type) {
            case LType.LTYPE_STRING:
                lType = new LString();
                break;
            case LType.LTYPE_BYTE:
                lType = new LByte();
                break;
            case LType.LTYPE_SHORT:
                lType = new LShort();
                break;
            case LType.LTYPE_INTEGER:
                lType = new LInteger();
                break;
            case LType.LTYPE_LONG:
                lType = new LLong();
                break;
            case LType.LTYPE_CHAR:
                lType = new LChar();
                break;
            case LType.LTYPE_FLOAT:
                lType = new LFloat();
                break;
            case LType.LTYPE_DOUBLE:
                lType = new LDouble();
                break;
            case LType.LTYPE_BOOLEAN:
                lType = new LBoolean();
                break;
            case LType.LTYPE_DATE:
                lType = new LDate();
                break;
            case LType.LTYPE_OBJECTID:
                lType = new LObjectId();
                break;
            default:
                throw new UnknowLTypeException("未知类型：" + type);
        }

        return lType;
    }

    /**
     * 获取对象的LType类型
     *
     * @param o 需要检查对象
     * @return LType对象
     */
    public static LType getLType(Object o) {
        String typeName = o.getClass().getName();
        LType lType;

        switch (typeName) {
            case "java.lang.String":
                lType = new LString();
                break;
            case "java.lang.Bytpe":
                lType = new LByte();
                break;
            case "java.lang.Short":
                lType = new LShort();
                break;
            case "java.lang.Integer":
                lType = new LInteger();
                break;
            case "java.lang.Long":
                lType = new LLong();
                break;
            case "java.lang.Char":
                lType = new LChar();
                break;
            case "java.lang.Float":
                lType = new LFloat();
                break;
            case "java.lang.Double":
                lType = new LDouble();
                break;
            case "java.lang.Boolean":
                lType = new LBoolean();
                break;
            case "java.util.Date":
                lType = new LDate();
                break;
            case "org.bson.types.ObjectId":
                lType = new LObjectId();
                break;
            default:
                throw new UnknowLTypeException("");
        }

        return lType;
    }

    /**
     * 将对象换为字符串,当对象为空时类型默认为LString
     *
     * @param object 待转换对象
     * @return 转换后的字符值
     */
    public static String toString(Object object) {

        if (object != null) {
            LType lType = getLType(object);
            return lType.toString(object);
        }

        return null;
    }
}
